package com.my.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.annotation.CleanCache;
import com.my.admin.entity.Role;
import com.my.admin.entity.RoleMenu;
import com.my.admin.service.RoleMenuService;
import com.my.admin.service.RoleService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService service;
    @Autowired
    private RoleMenuService rmService;

    @PostMapping("role/get")
    public Rest get(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody Role role) {
        return Rest.ok().setData("page", service.page(new Page<>(pageNo, pageSize), new QueryWrapper<Role>().
                like(StrUtil.isNotBlank(role.getName()), "name", role.getName()).like(StrUtil.isNotBlank(role.getRoleDesc()), "role_desc", role.getRoleDesc())));
    }

    @PostMapping("roles")
    public Rest add(@RequestBody Role role) {
        if(service.save(role)) {
            return Rest.ok("添加成功！");
        }else {
            return Rest.err("添加失败!");
        }
    }

    @PutMapping("roles")
    public Rest edit(@RequestBody Role role) {
        if(service.updateById(role)) {
            return Rest.ok("修改成功！");
        }else {
            return Rest.err("修改失败！");
        }
    }

    @DeleteMapping("roles")
    public Rest del(@RequestParam long id) {
        if(service.removeById(id)) {
            rmService.remove(new QueryWrapper<RoleMenu>().eq("role_id", id));
            return Rest.ok("删除成功！");
        }else {
            return Rest.err("删除失败！");
        }
    }

    @GetMapping("role/repeat")
    public Rest repeat(@RequestParam String name, @RequestParam(defaultValue = "-1") Long id) {
        QueryWrapper<Role> qw = new QueryWrapper<Role>().eq("name", name);
        if(id != -1) {
            qw.ne("id", id);
        }
        return Rest.ok().setData("flag", service.getOne(qw) != null);
    }

    @GetMapping("role/list")
    public Rest list() {
        return Rest.ok().setData("list", service.list());
    }

    @GetMapping("roleListByUserId")
    public Rest roleListByUserId(@RequestParam(defaultValue = "-1") Long userId) {
        return Rest.ok().setData("roles", service.roleListByUserId(userId));
    }

    @GetMapping("role/menuListByRoleId")
    public Rest menuListByRoleId(@RequestParam(defaultValue = "-1") Long roleId) {
        return Rest.ok().setData("tree", TreeUtil.build(service.menuListByRoleId(roleId), 0L, ((menu, tree) -> {
            tree.setId(menu.getId());
            tree.setParentId(menu.getParentId());
            tree.putExtra("title", menu.getName());
            tree.putExtra("checked", menu.isChecked());
            tree.putExtra("expand", menu.getDeep() < 2);
        })));
    }

    @PostMapping("role/roleMenu")
    @CleanCache(value = "com.my.admin.service.impl.MenuServiceImpl:getMenuByUserId:")
    public Rest userRole(@RequestParam Long roleId, @RequestParam(required = false) String[] menus) {
        rmService.remove(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
        if(menus == null || rmService.batchInsert(roleId, Convert.toLongArray(menus))) {
            return Rest.ok("角色菜单分配成功！");
        }
        return Rest.err("角色菜单分配失败！");
    }

}
